package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.MenuDto;
import com.project.hqshorttaxi.dto.RoleMenuDto;
import com.project.hqshorttaxi.dto.RoleMenuParam;
import com.project.hqshorttaxi.entity.RoleMenu;
import com.project.hqshorttaxi.mapper.RoleMenuMapper;
import com.project.hqshorttaxi.service.RoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<RoleMenu> getByMenuId(Integer menuId) {
        Example example = new Example(RoleMenu.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("menuId", menuId);
        c.andEqualTo("deleteFlag", 0);
        List<RoleMenu> list = roleMenuMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<RoleMenu> getByRoleId(Integer roleId) {
        Example example = new Example(RoleMenu.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("roleId", roleId);
        c.andEqualTo("deleteFlag", 0);
        List<RoleMenu> list = roleMenuMapper.selectByExample(example);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(RoleMenu entity) throws Exception {
        roleMenuMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(RoleMenu entity) throws Exception {
        roleMenuMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public List<RoleMenuDto> getMenuByRoleId(RoleMenuParam param) {
        List<MenuDto> menus = roleMenuMapper.getByRoleId(param);
        List<MenuDto> mainMenus = menus.stream().filter(item -> item.getPid() == 0).collect(Collectors.toList());
        List<RoleMenuDto> menuList = new ArrayList<RoleMenuDto>();
        for (MenuDto main : mainMenus) {
            RoleMenuDto dto = new RoleMenuDto();
            dto.setName(String.valueOf(main.getMenuId()));
            dto.setPath(main.getPath());
            dto.setAlwaysShow(true);
            dto.setComponent("Layout");
            dto.setRedirect("noRedirect");
            RoleMenuDto.Meta mainMeta = new RoleMenuDto.Meta();
            mainMeta.setIcon("menu");
            mainMeta.setTitle(main.getMenuName());
            dto.setMeta(mainMeta);
            List<RoleMenuDto.Children> childrenList = new ArrayList<RoleMenuDto.Children>();
            List<MenuDto> subMenus = menus.stream().filter(item -> item.getPid().equals(main.getMenuId())).collect(Collectors.toList());
            for (MenuDto sub : subMenus) {
                RoleMenuDto.Children children = new RoleMenuDto.Children();
                children.setPath(String.valueOf(sub.getMenuId()) + ":" + String.valueOf(sub.getType()));
                children.setName(String.valueOf(sub.getMenuId()));
                children.setComponent(sub.getPath());
                RoleMenuDto.Meta subMeta = new RoleMenuDto.Meta();
                subMeta.setTitle(sub.getMenuName());
                children.setMeta(subMeta);
                childrenList.add(children);
            }
            dto.setChildren(childrenList);
            menuList.add(dto);
        }
        return menuList;
    }
}
